package com.kayang.common.aop;

import com.kayang.common.db.Database;
import com.kayang.common.db.DatabaseFactory;
import com.kayang.common.exception.SPserviceException;
import com.kayang.common.languages.MSGAlarm;
import com.kayang.nadapt.util.JsonConvert;
import com.kayang.nadapt.util.StringUtil;
import org.springframework.stereotype.Service;

@Service
public class SPservice {

    public void before(String spname, BaseContext context){
        Database db = DatabaseFactory.getDatabase();
        Object o = db.execute(spname, 1, JsonConvert.serializeObject(context));
        if(StringUtil.isNullOrEmpty(o)){
            MSGAlarm msgAlarm = MSGAlarm.create(o, db, 2052);
            if(msgAlarm.getHasAlarm()){
                throw new SPserviceException(msgAlarm);
            }
        }
    }

    public void after(String spname, BaseContext context){
        Database db = DatabaseFactory.getDatabase();
        Object o = db.execute(spname, 2, JsonConvert.serializeObject(context));
        if(StringUtil.isNullOrEmpty(o)){
            MSGAlarm msgAlarm = MSGAlarm.create(o, db, 2052);
            if(msgAlarm.getHasAlarm()){
                throw new SPserviceException(msgAlarm);
            }
        }
    }
}
